<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>01_引出声明周期</title>
</head>

<body>
    <div id="app"></div>

    <script src="../../00_课程资料/js/react.development.js"></script>
    <script src="../../00_课程资料/js/react-dom.development.js"></script>
    <script src="../../00_课程资料/js/babel.min.js"></script>


    <script type="text/babel">
        class LifeComponent extends React.Component {
            state = {
                fontSize: 12
            }
            // 点击关闭按钮
            unMount = () => {
                // 卸载组件,调用固定函数 
                ReactDOM.unmountComponentAtNode(document.getElementById("app"))
            }
            // 组件挂在完成后触发
            componentDidMount() {
                this.timer = setInterval(() => {
                    let { fontSize } = this.state
                    fontSize += 1
                    if (fontSize >= 40) fontSize = 12
                    this.setState({
                        fontSize: fontSize
                    })
                }, 200);
            }

            // 组件卸载前触发，此时还没真正卸载
            componentWillUnmount() {
                // 卸载前清除定时器
                clearInterval(this.timer)
            }

            // 渲染页面
            render() {
                let { fontSize } = this.state
                return (
                    <div>
                        <h2 style={{ fontSize: fontSize + 'px', transition: '1s' }}>我会自己变大变小</h2>
                        <button onClick={this.unMount}>关闭</button>
                    </div>
                )
            }
        }

        ReactDOM.render(<LifeComponent />, document.getElementById("app"))
    </script>
</body>

</html>